package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {




    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    /**
     * 添加菜品
     * @param dishDTO
     */
    @Override
    public void insertDishWithFlavors(DishDTO dishDTO) {
// dishDto->dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //补状态
        dish.setStatus(StatusConstant.DISABLE);
        //菜品表 调用DishMapper
        dishMapper.insert(dish);


        // 口味数据 调用Dishflavormapper
        Long dishId = dish.getId();

        List<DishFlavor> flavors=dishDTO.getFlavors();
        if (flavors!=null&&flavors.size()>0){
            // 遍历所有的口味，并给口味设置 菜品id
            flavors.forEach(flavor -> flavor.setDishId(dishId));
            dishFlavorMapper.insertFlavors(flavors);

        }


    }

    /**
     * 菜品 分页查询条件
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO,dish);
        Page<DishVO> pageResult=dishMapper.selectByCondition(dish);
        return new PageResult(pageResult.getTotal(), pageResult.getResult());
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    public void deleteById(List<Long> ids) {
        if (ids == null || ids.size() == 0) {
            return;
        }

        //判断菜品 是否起售 批量判断
        int enableCount = dishMapper.countEnableByIds(ids);

        if (enableCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }

        // 判断id是否被套餐关联 批量判断
       int setMealCount = setmealDishMapper.countByDishIds(ids);
        if (setMealCount > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);

        }
        // 批量删除
dishMapper.deleteByIds(ids);
        // 批量删除口味
        dishFlavorMapper.deleteByDishIds(ids);


    }

    /**
     * 根据id查询菜品的所有信息
     * @param id
     * @return
     */
    @Override
    public DishVO selectById(Long id) {
        Dish dish =dishMapper.selectById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        // 从口味表查出 对应菜品的口味数据

             List<DishFlavor> flavors =dishFlavorMapper.selectByDishId(id);
             dishVO.setFlavors(flavors);

        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    public void updateDishWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //补状态
        dish.setStatus(StatusConstant.DISABLE);
        //菜品表 调用DishMapper
        dishMapper.update(dish);

        // 修改口味数据

        // 删除所有口味
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(dish.getId());

        dishFlavorMapper.deleteByDishIds(ids);
        //新增所有口味
        // 口味数据 调用Dishflavormapper
        Long dishId = dish.getId();

        List<DishFlavor> flavors=dishDTO.getFlavors();
        if (flavors!=null&&flavors.size()>0){
            // 遍历所有的口味，并给口味设置 菜品id
            flavors.forEach(flavor -> flavor.setDishId(dishId));
            dishFlavorMapper.insertFlavors(flavors);

        }
    }
    /**
     * 起售或停售菜品
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder().id(id).status(status).build();
        dishMapper.update(dish);
    }

    /**
     * 根据分类查询起售中菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> listByCategoryId(Long categoryId) {
        Dish dish=Dish
                .builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }


  /*  @Transactional
    @Override
    public void startOrStop(Integer status, Long id) {
       Dish dish=Dish.builder()
               .id(id)
               .status(status)
               .build();
       dishMapper.update(dish);
       if (status.equals(StatusConstant.DISABLE)){
           // 联表查询setmeal、dish、setmeal_dish
           List<Setmeal> setmealList =setmealMapper.getByDishId(id);
           for (Setmeal setmeal : setmealList) {
               if (setmeal !=null){
                   setmeal.setStatus(StatusConstant.DISABLE);
                  setmealMapper.update(setmeal);
               }
           }
       }

    }*/

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
